﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Practices.Composite.Events;
using Scoreboard.Basement.Events;

namespace Scoreboard.Basement.ViewModels
{
	public abstract class UpdateableViewModel : ViewModelBase
	{		
		protected Dictionary<string, Action> Updaters { get; private set; }

		protected UpdateableViewModel()
		{
			Updaters = new Dictionary<string, Action>();

			var eventAggregator = ServiceLocator.Get<IEventAggregator>();
			var dataChangedEvent = eventAggregator.GetEvent<DataChangedEvent>();
			dataChangedEvent.Subscribe(InvokeUpdater);

			var userAuthenticatedEvent = eventAggregator.GetEvent<UserAuthenticatedEvent>();
			userAuthenticatedEvent.Subscribe(UserAuthenticatedHandler);
		}

		public void UserAuthenticatedHandler(bool isRegistered)
		{
			InvokeUpdater(EntityTypes.CurrentUser);
		}

		public void InvokeUpdater(string entityType)
		{
			Action updater;
			var result = Updaters.TryGetValue(entityType, out updater);
			if (result)
			{
				updater();
			}
		}

		protected void AddUpdater(string entityType, Action action)
		{
			Updaters.Add(entityType, action);
		}

		protected virtual string Name
		{
			get
			{
				return "Base";
			}
		}
	}
}
